/**
 * 
 */
package com.bolbachchan.blog.dao.posts;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bolbachchan.blog.dao.common.BaseDAO;
import com.bolbachchan.blog.dto.PostDTO;
import com.bolbachchan.blog.hibernate.domain.Posts;

/**
 * @author Chaitanya
 * 
 */
public class UserPostsDAOImpl extends BaseDAO implements UserPostsDAO {

    private static final Logger LOG = LoggerFactory.getLogger(UserPostsDAOImpl.class);

    /*
     * (non-Javadoc)
     * @see
     * com.bolbachchan.blog.dao.posts.UserPostsDAO#saveNewPost(com.bolbachchan
     * .blog.dto.PostDTO)
     */
    @Override
    public void saveNewPost(PostDTO dto) {
	Posts post = null;
	Session session = null;
	try {
	    session = getSessionFactory().getCurrentSession();
	    post = new Posts();

	    post.setUserDetails(dto.getUserDetails());
	    post.setPostTitle(dto.getPostTitle());
	    post.setPostContent(dto.getPostContent());

	    Timestamp updateDt = new Timestamp(new Date().getTime());

	    post.setCreateDt(updateDt);
	    post.setUpdateDt(updateDt);

	    LOG.debug("The new post is : " + post);

	    int id = (Integer) session.save(post);

	    LOG.debug("The OBJ_ID of the new post inserted is : " + id);
	}
	catch (Exception e) {
	    LOG.error("Error occured while saving the new post: " + e);
	}
	finally {
	    LOG.debug("Finished saveNewPost >> UserPostsDAOImpl");
	}
    }

    /*
     * (non-Javadoc)
     * @see
     * com.bolbachchan.blog.dao.posts.UserPostsDAO#updatePost(com.bolbachchan
     * .blog.dto.PostDTO)
     */
    @Override
    public void updatePost(PostDTO dto) {
	Query query = null;
	Map<String, Object> parameterMap = getParameterMap();
	parameterMap.put(POST_TITLE, dto.getPostTitle());
	parameterMap.put(POST_CONTENT, dto.getPostContent());
	parameterMap.put(UPDATE_DT, new Timestamp(new Date().getTime()));
	parameterMap.put(OBJ_ID, dto.getObjId());
	try {
	    query = getNamedQuery(UPDATE_POST, parameterMap);

	    query.executeUpdate();
	}
	catch (Exception e) {
	    LOG.error("Error occured while updating the post: " + e);
	}
	finally {
	    if (query != null) {
		query = null;
	    }
	    clearParameterMap();
	    LOG.debug("Finished updatePost >> UserPostsDAOImpl");
	}
    }

    /*
     * (non-Javadoc)
     * @see
     * com.bolbachchan.blog.dao.posts.UserPostsDAO#getPostsByUserId(com.bolbachchan
     * .blog.dto.PostDTO)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Posts> getPostsByUserId(PostDTO dto) {

	Query query = null;
	List<Posts> postsList = null;
	Map<String, Object> parameterMap = getParameterMap();
	parameterMap.put(USER_OBJ_ID, dto.getUserObjId());
	try {
	    LOG.debug("Entering getPostsByUserId >> UserPostsDAOImpl");
	    query = getNamedQuery(GET_POSTS_BY_USER_ID, parameterMap);
	    postsList = (List<Posts>) query.list();
	}
	catch (Exception e) {
	    LOG.error("Error occured while getting Posts : " + e);
	}
	finally {
	    if (query != null) {
		query = null;
	    }
	    clearParameterMap();
	    LOG.debug("Finished getPostsByUserId >> UserPostsDAOImpl");
	}
	return postsList;
    }

    @Override
    public Posts getPostByPostId(int objId) {
	Query query = null;
	Posts post = new Posts();
	Map<String, Object> parameterMap = getParameterMap();
	parameterMap.put(OBJ_ID, objId);
	try {
	    LOG.debug("Entering getPostByPostId >> UserPostsDAOImpl");
	    query = getNamedQuery(GET_POST_BY_POST_ID, parameterMap);
	    post = (Posts) query.uniqueResult();

	}
	catch (Exception e) {
	    LOG.error("Error occured while retrieving Post : " + e);
	}
	finally {
	    if (query != null) {
		query = null;
	    }
	    clearParameterMap();
	    LOG.debug("Exiting getPostByPostId >> UserPostsDAOImpl");
	}
	return post;
    }

    @Override
    public void saveNewPost(Posts post) {
	Session session = null;
	try {
	    LOG.debug("Entering saveNewPost >> UserPostsDAOImpl");
	    session = getSessionFactory().getCurrentSession();
	    session.save(post);
	}
	catch (Exception e) {
	    LOG.error("Error occured while saving a new post : " + e);
	}
	finally {
	    LOG.debug("Finished saveNewPost >> UserPostsDAOImpl");
	}
    }

    @Override
    public void updatePost(Posts post) {
	Session session = null;
	try {
	    LOG.debug("Entering updatePost >> UserPostsDAOImpl");
	    session = getSessionFactory().getCurrentSession();
	    session.update(post);
	}
	catch (Exception e) {
	    LOG.error("Error occured while updating the post : " + e);
	}
	finally {
	    LOG.debug("Finished updatePost >> UserPostsDAOImpl");
	}
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Posts> getPostsByUserId(int userId) {
	Query query = null;
	List<Posts> posts = null;
	Map<String, Object> parameterMap = getParameterMap();
	parameterMap.put(USER_OBJ_ID, userId);
	try {
	    LOG.debug("Entering getPotsByUserId >> UserPostsDAOImpl");
	    query = getNamedQuery(GET_POSTS_BY_USER_ID, parameterMap);
	    posts = (List<Posts>) query.list();
	}
	catch (Exception e) {
	    LOG.error("Error occured while getting Posts : " + e);
	}
	finally {
	    if (query != null) {
		query = null;
	    }
	    clearParameterMap();
	    LOG.debug("Finished getPostsByUserId >> UserPostsDAOImpl");
	}
	return posts;
    }

}
